package com.ctrl.riveradministrator.mvp.view.activity.information;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BaseListAdapter;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.mvp.model.Information;
import com.ctrl.riveradministrator.mvp.model.WorkDynamic;
import com.ctrl.riveradministrator.mvp.presenter.InformationPresenter;

import java.util.ArrayList;
import java.util.List;

public class InformationActivity extends BaseActivity<InformationPresenter> implements InformationView,
        BaseListAdapter.OnItemClickListener<Information>, SwipeRefreshLayout.OnRefreshListener {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;

    private RecyclerView infoDynRl;
    private SwipeRefreshLayout infoDynSfl;
    private LinearLayoutManager layoutManager;
    private InformationAdapter informationAdapter;

    private List<Information> informations = new ArrayList<>();
    private static int currentPage;

    @Override
    protected void initParams(Bundle parms) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_information;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = $(R.id.toolbar);
        titleTv = $(R.id.title_tv);
        leftMenu = $(R.id.left_iv);
        infoDynRl = $(R.id.info_list);
        infoDynSfl = $(R.id.info_srl);

        layoutManager = new LinearLayoutManager(this);
        infoDynRl.setLayoutManager(layoutManager);
        infoDynSfl.setEnabled(false);
        infoDynSfl.setColorSchemeColors(Color.RED, Color.BLUE);
        informationAdapter = new InformationAdapter(this);
        infoDynRl.setAdapter(informationAdapter);

        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("加载中...");
        pDialog.setCancelable(false);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(getString(R.string.information));
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    @Override
    protected void doBusiness(Context mContext) {
        currentPage = 0;
        infoDynRl.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (infoDynRl.computeVerticalScrollOffset() > 0&&infoDynRl.computeVerticalScrollExtent() + infoDynRl.computeVerticalScrollOffset()
                            >= infoDynRl.computeVerticalScrollRange()) {
                        currentPage++;
                        mPresenter.getDynInfo("" + currentPage, "20");
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mPresenter.getDynInfo(currentPage + "", "20");
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        informationAdapter.setOnItemClickListener(this);
        infoDynSfl.setOnRefreshListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected InformationPresenter createPresenter() {
        return new InformationPresenter(this);
    }

    @Override
    public void showMessage(List<Information> information) {
        if (currentPage == 0) {
            this.informations.clear();
        }
        this.informations.addAll(information);
        informationAdapter.updateListView(informations);
    }

    @Override
    public void onItemClick(Information information, int position) {
        startActivity(new Intent(this, WebActivity.class).putExtra("information", information));
    }

    @Override
    public void showLoading() {
        super.showLoading();
        //pDialog.show();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        //pDialog.cancel();
    }

    @Override
    public void onRefresh() {
        currentPage = 0;
        mPresenter.getDynInfo("0", "20");
    }
}
